package cn.javacv.third.oss;

import cn.javacv.gulimall.common.utils.R;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;


@RestController
@RequestMapping("/third")
public class OssController {
    @Autowired
    private OSS ossClient;

    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endpoint;

    @Value("${spring.cloud.alicloud.access-key}")
    private String accessKey;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucket;

    @PostMapping("/fileUpload")
    public R fileUpload(@RequestBody MultipartFile multipartFile) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, accessKey, new File("C:\\Users\\29521\\Documents\\Tencent Files\\295213096\\FileRecv\\「动态规划」问题思考方向.png"));
        // 上传文件。
        ossClient.putObject(putObjectRequest);


        // 关闭OSSClient。
        ossClient.shutdown();
        return R.ok();
    }

    @GetMapping("/oss/policy")
    public R getPolicy() {
        String host = "http://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
        Map<String, String> respMap = new LinkedHashMap<>();
        String fileDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, fileDir);

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            respMap.put("accessid", accessKey);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", fileDir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
            // respMap.put("expire", formatISO8601Date(expiration));

        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
        }
        return R.ok().put("data",respMap);

    }
}
